import traceback

from server_init import get_connection


def get_timestamp(line_split_item):
    try:
        timestamp = int(line_split_item)
    except:
        traceback.print_exc()
        timestamp = ''
        for ch in line_split_item:
            if '9' >= ch >= '0':
                timestamp += ch
                if len(timestamp) == 10:
                    timestamp = int(timestamp)
                    break
    return timestamp


def get_table_name_by_secret_card(secret_card):
    sql = 'select table_index from gate_pass_info_index where secret_card=%s'
    cursor = get_connection().cursor()
    count = cursor.execute(sql, secret_card)
    if count <= 0:
        print('can not find secret_card')
        return -1
    return cursor.fetchone()[0]

    # if secret_card not in gate_pass_info_index_cache:
    #     print('can not find secret_card in gate_pass_info_index_cache')
    #     return -1
    # return gate_pass_info_index_cache[secret_card]


def get_table_name_by_device(device):
    sql = 'select table_index from gate_pass_info_device_index where gate_id=%s'
    cursor = get_connection().cursor()
    count = cursor.execute(sql, device)
    if count <= 0:
        print('can not find device')
        return -1
    return cursor.fetchone()[0]
